﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;


namespace ATA.Winforms.Report
{
    public partial class frmRptView : Form
    {
        public frmRptView(int iIndex)
        {
            try
            {
                InitializeComponent();

                DataSet dtsData = new DataSet();
                ReportDocument oRpt = new ReportDocument();

                string strDirPath;
                string strRptName = "";
                strDirPath = Directory.GetCurrentDirectory();
                //strDirPath = "..\\Reports";
                switch (iIndex)
                {
                    case 0:
                        strRptName = "rpt000.rpt";
                        break;
                    case 1:
                        strRptName = "rpt001.rpt";
                        break;
                    case 2:
                        strRptName = "rpt002.rpt";
                        break;
                    case 3:
                        strRptName = "rpt003.rpt";
                        break;
                    case 4:
                        strRptName = "rpt004.rpt";
                        break;
                    case 5:
                        strRptName = "rpt005.rpt";
                        break;
                    case 6:
                        strRptName = "rpt006.rpt";
                        break;
                    default:
                        MessageBox.Show("Không có báo cáo nào tương ứng !");
                        break;
                }
                strDirPath = strDirPath + "\\Reports\\" + strRptName;
                oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                //oRpt.SetDataSource(dtsData);
                rptView.ReportSource = oRpt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public frmRptView(int iIndex, string pv_strBranchID, string pv_strCustomerID, int pv_CustomerLevel,
                            int pv_iMonth, int pv_iYear, int pv_iBrandID, int pv_iChuongTrinhDuThuongID,
                            string pv_strChuongTrinhChietKhau, int pv_iLoaiMayPhaMau)
        {
            try
            {
                InitializeComponent();

                DataSet dtsData = new DataSet();
                //ReportDocument oRpt = new ReportDocument();

                string strDirPath;
                string strRptName = "";
                strDirPath = Directory.GetCurrentDirectory();
                //Guid v_guidCustomerID = new Guid(pv_strCustomerID);
                Guid v_guidBranchID;

                int v_iQuarter = (int)((pv_iMonth - 1) / 3) + 1;

                //if (pv_strCustomerID.Trim() != string.Empty)
                //{
                //    v_guidCustomerID = new Guid(pv_strCustomerID);
                //}
                //else
                //    v_guidCustomerID = Guid.Empty;

                if (pv_strBranchID.Trim() != string.Empty)
                {
                    v_guidBranchID = new Guid(pv_strBranchID);
                }
                else
                    v_guidBranchID = Guid.Empty;

                string v_strServerName = ConfigurationManager.AppSettings["reportServer"];
                string v_strDatabaseName = ConfigurationManager.AppSettings["reportDatabase"];
                string v_strUserName = ConfigurationManager.AppSettings["reportUserName"];
                string v_strPassword = ConfigurationManager.AppSettings["reportPassword"];

                switch (iIndex)
                {
                    case 0:
                        strRptName = "rptThongKeChietKhauThang.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        oRpt.ParameterFields["@ChietKhauID"].CurrentValues.AddValue(pv_strChuongTrinhChietKhau);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);
                        oRpt.ParameterFields["@TransactionFee"].CurrentValues.AddValue(0.5);
                        break;
                    case 1:
                        strRptName = "rptThongKeChietKhauNam.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);
                        break;
                    case 2:
                        strRptName = "rptChietKhauDacBiet.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);
                        break;
                    case 3:
                        strRptName = "rptTheoDoiChuongTrinhThuong1Thang.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        break;
                    case 4:
                        strRptName = "rptTheoDoiChuongTrinhThuong1Quy.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        break;
                    case 5:
                        strRptName = "rptTheoDoiChuongTrinhThuong1Nam.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        break;
                    case 6:
                        strRptName = "rptTheoDoiChuongTrinhThuong3Nam.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        break;
                    case 7:
                        strRptName = "rptTheodoiSanPhamBASE.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);

                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        break;
                    case 8:
                        strRptName = "rptTheodoiATAFastDaiLy.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        break;
                    case 9:
                        strRptName = "rptTheodoiATAFastNhaThau.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);
                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        break;
                    case 10:
                        strRptName = "rptTheoDoiChuongTrinhXe.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByDefault);

                        //oRpt.ParameterFields["@ChuongTrinhID"].CurrentValues.AddValue(pv_iChuongTrinhDuThuongID);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);
                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        //oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);
                        break;
                    case 11:
                        strRptName = "rptTheoDoiChuongTrinhLapMayPhaMau.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);

                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        break;
                    case 12:
                        strRptName = "rptXacNhanCongNo.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@AccountingObjectId"].CurrentValues.AddValue(pv_strCustomerID);
                        }
                        else
                            oRpt.ParameterFields["@AccountingObjectId"].CurrentValues.AddValue(null);

                        break;
                    case 13:
                        strRptName = "rptTongHopCacCTDaiLy.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        Guid v_guidCustomerID;
                        if (pv_strCustomerID.Trim() != string.Empty)
                        {
                            v_guidCustomerID = new Guid(pv_strCustomerID);
                        }
                        else
                            v_guidCustomerID = Guid.Empty;

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        oRpt.ParameterFields["@ChietKhauID"].CurrentValues.AddValue(pv_strChuongTrinhChietKhau);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        if (!v_guidCustomerID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@KhachHangID"].CurrentValues.AddValue(pv_strCustomerID);
                        }
                        else
                            oRpt.ParameterFields["@KhachHangID"].CurrentValues.AddValue(null);

                        oRpt.ParameterFields["@TransactionFee"].CurrentValues.AddValue(0.5);
                        break;
                    case 14:
                        strRptName = "rptTongKetCacCT.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Month"].CurrentValues.AddValue(pv_iMonth);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);
                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        break;
                    case 15:
                        strRptName = "rptTongHopPhanTichDoanhSoQuy.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Quarter"].CurrentValues.AddValue(v_iQuarter);
                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);
                        break;
                    case 16:
                        strRptName = "rptTongHopPhanTichDoanhSoNam.rpt";

                        strDirPath = strDirPath + "\\Reports\\" + strRptName;
                        oRpt.Load(strDirPath, OpenReportMethod.OpenReportByTempCopy);

                        oRpt.ParameterFields["@Year"].CurrentValues.AddValue(pv_iYear);
                        if (!v_guidBranchID.Equals(Guid.Empty))
                        {
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(pv_strBranchID);
                        }
                        else
                            oRpt.ParameterFields["@BranchID"].CurrentValues.AddValue(null);

                        oRpt.ParameterFields["@BrandID"].CurrentValues.AddValue(pv_iBrandID);

                        break;
                    default:
                        MessageBox.Show("Không có báo cáo nào tương ứng !");
                        break;
                }

                oRpt.SetDatabaseLogon(v_strUserName,
                                        v_strPassword,
                                        v_strServerName,
                                        v_strDatabaseName);

                foreach (CrystalDecisions.CrystalReports.Engine.Table table in oRpt.Database.Tables)
                {
                    TableLogOnInfo logOnInfo = table.LogOnInfo;
                    if (logOnInfo != null)
                    {
                        table.LogOnInfo.TableName = table.Name;
                        table.LogOnInfo.ConnectionInfo.UserID = v_strUserName;
                        table.LogOnInfo.ConnectionInfo.Password = v_strPassword;
                        table.LogOnInfo.ConnectionInfo.DatabaseName = v_strDatabaseName;
                        table.LogOnInfo.ConnectionInfo.ServerName = v_strServerName;
                        table.LogOnInfo.ConnectionInfo.AllowCustomConnection = true;
                        table.LogOnInfo.ConnectionInfo.IntegratedSecurity = false;
                        table.ApplyLogOnInfo(table.LogOnInfo);
                    }
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void frmRptView_Load(object sender, EventArgs e)
        {
            try
            {
                rptView.ReportSource = oRpt;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
